/**
 * 
 */
package cn.ad.server.web.spring.service.biz;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Wu Chengjun
 */
@Service
public class TrackingServiceImpl implements TrackingService {

	/**
	 * 默认跟踪参数字符串模板
	 */
	public static final String DEFAULT_TRACKING_QUERYSTRING_FORMAT = "version=%s&pid=%s&aid=%s&sid=%s&cid=%s&mid=%s&guid=%s";

	/**
	 * 广告展示参数格式模板
	 */
	public String imp_querystring_format = DEFAULT_TRACKING_QUERYSTRING_FORMAT;

	/**
	 * 广告点击参数格式模板
	 */
	public String click_querystring_format = DEFAULT_TRACKING_QUERYSTRING_FORMAT + "&target=%s";

	/**
	 * 竞价成功参数格式模板
	 */
	public String winotice_querystring_format = DEFAULT_TRACKING_QUERYSTRING_FORMAT;

	/**
	 * 当前接口版本号
	 */
	private String default_version = "2.0";

	/**
	 * 监测代码服务地址，一般指服务器http访问的域名或者主机与端口，格式为http://
	 */
	@Resource(name = "serverAddress")
	private String trackingServerAddr;

	/**
	 * 渠道路径字符
	 */
	private String channelPath = "/ch";

	/**
	 * 广告展示path路径
	 */
	private String impressionPath = "/imp";

	/**
	 * 点击path路径
	 */
	private String clickPath = "/click";

	/**
	 * 竞价成功path路劲
	 */
	private String winoticePath = "/winotice";

	/**
	 * 接口版本路劲
	 */
	private String versionPath = "/v2";

	/**
	 * os == 2是ios系统，所有的接口返回https
	 * 
	 * @param os
	 * @param url
	 * @return
	 */
	String converHttps(int os, String url) {
		if (os == 2)
			url = url.replace("http:", "https:");
		return url;
	}

	/*
	 * 当前传入version 为null时，默认版本号
	 * 
	 * @see
	 * cn.falconnect.adserver.adapter.TrackingService#
	 * buildClickQueryString(java.lang.String, int, int, int, int, int, int,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public String buildClickQueryString(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                        long creativeId, long materialId, String guid, String targetUrl, int os) {
		String url = String.format(click_querystring_format, version == null ? this.default_version : version,
				platformId, adspaceId == 0 ? "" : adspaceId, strategyId, creativeId, materialId, guid, targetUrl);
		return converHttps(os, url);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.falconnect.adserver.adapter.TrackingService#
	 * buildImpressionQueryString(java.lang.String, int, int, int, int, int,
	 * int, java.lang.String)
	 */
	@Override
	public String buildImpressionQueryString(String version, int channelId, int platformId, long adspaceId,
                                             long strategyId, long creativeId, long materialId, String guid, int os) {
		String url = String.format(imp_querystring_format, version == null ? this.default_version : version, platformId,
				adspaceId, strategyId, creativeId, materialId, guid);
		return converHttps(os, url);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.falconnect.adserver.adapter.TrackingService#
	 * buildImpressionUrl(java.lang.String, int, int, int, int, int, int,
	 * java.lang.String)
	 */
	@Override
	public String buildImpressionUrl(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                     long creativeId, long materialId, String guid, int os) {
		StringBuilder sb = new StringBuilder();
		sb.append(getTrackingURLPrefix(channelId, impressionPath, os)).append(buildImpressionQueryString(version,
				channelId, platformId, adspaceId, strategyId, creativeId, materialId, guid, os));
		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.falconnect.adserver.adapter.TrackingService#
	 * buildClickUrl(java.lang.String, int, int, int, int, int,
	 * java.lang.String)
	 */
	@Override
	public String buildClickUrl(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                long creativeId, long materialId, String guid, String targetUrl, int os) {
		StringBuilder sb = new StringBuilder();
		sb.append(getTrackingURLPrefix(channelId, clickPath, os)).append(buildClickQueryString(version, channelId,
				platformId, adspaceId, strategyId, creativeId, materialId, guid, targetUrl, os));
		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.falconnect.adserver.adapter.TrackingService#
	 * buildWinoticeUrl(java.lang.String, int, int, int, int, int, int,
	 * java.lang.String)
	 */
	@Override
	public String buildWinoticeUrl(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                   long creativeId, long materialId, String guid, int os) {
		String queryString = String.format(winotice_querystring_format, version, platformId, adspaceId, strategyId,
				creativeId, materialId, guid);
		StringBuilder sb = new StringBuilder();
		sb.append(getTrackingURLPrefix(channelId, winoticePath, os)).append(queryString);
		return sb.toString();
	}

	/**
	 * 获取tracking URL 前缀
	 * 
	 * @return
	 */
	private String getTrackingURLPrefix(int channelId, String trackingPath, int os) {
		StringBuilder sb = new StringBuilder();
		sb.append(getTrackingServerAddr()).append(versionPath).append(trackingPath).append(channelPath).append("/")
				.append(channelId).append("?");
		if (os == 2)
			sb.replace(0, 4, "https");
		return sb.toString();
	}

	/**
	 * 当提供的地址是以 / 结尾的，则返回去掉 / 的字符串
	 * 
	 * @return the trackingServerAddr
	 */
	public String getTrackingServerAddr() {
		return trackingServerAddr.endsWith("/") ? trackingServerAddr.substring(0, trackingServerAddr.length() - 1)
				: trackingServerAddr;
	}

	/**
	 * @param trackingServerAddr
	 *            the trackingServerAddr to set
	 */
	public void setTrackingServerAddr(String trackingServerAddr) {
		this.trackingServerAddr = trackingServerAddr;
	}

	/**
	 * @return the impressionPath
	 */
	public String getImpressionPath() {
		return impressionPath;
	}

	/**
	 * @param impressionPath
	 *            the impressionPath to set
	 */
	public void setImpressionPath(String impressionPath) {
		this.impressionPath = impressionPath;
	}

	/**
	 * @return the clickPath
	 */
	public String getClickPath() {
		return clickPath;
	}

	/**
	 * @param clickPath
	 *            the clickPath to set
	 */
	public void setClickPath(String clickPath) {
		this.clickPath = clickPath;
	}

	/**
	 * @return the winoticePath
	 */
	public String getWinoticePath() {
		return winoticePath;
	}

	/**
	 * @param winoticePath
	 *            the winoticePath to set
	 */
	public void setWinoticePath(String winoticePath) {
		this.winoticePath = winoticePath;
	}

	/**
	 * @return the versionPath
	 */
	public String getVersionPath() {
		return versionPath;
	}

	/**
	 * @param versionPath
	 *            the versionPath to set
	 */
	public void setVersionPath(String versionPath) {
		this.versionPath = versionPath;
	}

	/**
	 * @return the channelPath
	 */
	public String getChannelPath() {
		return channelPath;
	}

	/**
	 * @param channelPath
	 *            the channelPath to set
	 */
	public void setChannelPath(String channelPath) {
		this.channelPath = channelPath;
	}

	/**
	 * @return the default_version
	 */
	public String getDefault_version() {
		return default_version;
	}

	/**
	 * @param default_version
	 *            the default_version to set
	 */
	public void setDefault_version(String default_version) {
		this.default_version = default_version;
	}

	@Override
	public String buildClickUrl2(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                 long creativeId, long materialId, String guid, String targetUrl, String extend, int os) {
		return buildClickUrl(version, channelId, platformId, adspaceId, strategyId, creativeId, materialId, guid,
				targetUrl, os) + extend;
	}

	@Override
	public String buildClickQueryString2(String version, int channelId, int platformId, long adspaceId, long strategyId,
                                         long creativeId, long materialId, String guid, String targetUrl, String extend, int os) {

		return buildClickQueryString(version, channelId, platformId, adspaceId, strategyId, creativeId, materialId,
				guid, targetUrl, os) + extend;
	}
}
